/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Define class TCPCommunicateTask.
//
// TCPCommunicateTask is a version of CommunicateTask under TCP protocol.
//

#ifndef __TCP__COMMUNICATE__TASK__H
#define __TCP__COMMUNICATE__TASK__H

#include "communicate_task.h"
#include "tcp_socket_communicator.h"
#include "communicate_configure.h"
#include "byte_buffer_dynamic.h"

namespace amz {

class TCPCommunicateTaskPool;

class TCPCommunicateTask : public ApplicationTask {
 public:
  TCPCommunicateTask(int socket, const TCPCommunicateTaskPool *pool,
         const TCPCommunicateConfigure &configure);
  virtual ~TCPCommunicateTask();

  // Register/Unregister to epoll.
  bool Register(Epoller &epoller, int events);
  bool Unregister(Epoller &epoller);

  // Communicate with application layer.
  virtual bool SendApplication();
  virtual bool ReceiveApplication();

  // Communicate with socket.
  virtual bool Send();
  virtual bool Receive();

 protected:
  TCPCommunicateTaskPool *pool_;
  TCPSocketCommunicator *communicator_;

 private:
  void InitializeCommunicator(const TCPCommunicateConfigure &configure);
  void FinalizeCommunicator();

  SimpleByteBufferDynamic temp_buffer_;

  // Second store, use to store a message when the ring queue uses nonblocking
  // pattern and bound pattern.
  ByteBufferDynamic second_store_;
};

}  // namespace amz

#endif  // __TCP__COMMUNICATE__TASK__H

